package CreateThreadPool;

import java.util.List;
import java.util.concurrent.*;
public class CreateThreadPoolExecutor {
    public static void main(String[] args) {
        Executor threadPool = new ThreadPoolExecutor(3,//核心线程数量
                5,//最大线程数量
                8,//临时线程存活时间
                TimeUnit.SECONDS,//临时线程存活时间单位
                new ArrayBlockingQueue<>(4),//创建任务队列，最大存放任务数量为4
                Executors.defaultThreadFactory(),//线程工厂，用到了Executors工具类来创建默认线程工厂
                new ThreadPoolExecutor.AbortPolicy());//拒绝策略，默认策略:抛出异常

        //定义出Runnable类型的任务
        MyRunnable1 myRunnable1 = new MyRunnable1();
        MyRunnable1 myRunnable2 = new MyRunnable1();
        MyRunnable1 myRunnable3 = new MyRunnable1();
        MyRunnable1 myRunnable4 = new MyRunnable1();
        MyRunnable1 myRunnable5 = new MyRunnable1();
        //将该任务提交给线程池
        threadPool.execute(myRunnable1);
        threadPool.execute(myRunnable2);
        threadPool.execute(myRunnable3);
        threadPool.execute(myRunnable4);
        threadPool.execute(myRunnable5);

/*        //当全部任务都执行结束后，才会关闭
        ((ThreadPoolExecutor) threadPool).shutdown();*/
/*        List<Runnable> l = ((ThreadPoolExecutor) threadPool).shutdownNow();
        for (int i = 0; i < l.size(); i++) {
            System.out.println("还没来得及执行的任务:" + l.get(i));
        }*/
    }
}

class  MyRunnable1 implements Runnable{
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "正在执行");
    }
}